import requests
import time
import execjs

timestamp = str(int(time.time() * 1000))

cookies = {
    'BAIDUID_BFESS': '5529DAA71C261A93C6FEE562D1C92849:SL=0:NR=10:FG=1'
}

headers = {
    'Accept': '*/*',
    'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
    'Acs-Token': '1724310007693_1724337746669_yKJsXVPDy6tSFQfUIJMcEVPhlV+L2CMID4oYoL/oTitI+rO7Y+/mAwH6H1xnkAucikiEuRPb0xKin8t1Y/WAVQEoe1M7wYwQsr7ttgdPE4H+v6n3htujlGqOhQWQnbB7wqP+sKSfdRB8qhD7KDlL4dUTMJxv8rfUCJASzuF7VhBTJB/prAATXCCPu4EXgon7zq4SJF0fBXvVqlEZIo0FY275vQjDOuuKaRm8umJqMp1xUIFqmmelSl10i8W/bZlrc8qb6mol5sBKm/goifQhV/LKCcQnVQwYZTnOd8u9h4mYQtyjiLLg4M4pLjOd0LZSpDhrbXZK4iZ+qQzNWLc9efSm4189tjilbeqyVcho1od2fStTr3FHSi06mTxn8V3yH7pIgmouwutGAE+VyrMmTxiQHZZNu3V6gLSiBUjZw7Z7Vp2XaXqXMVs/qI5eMFQNQa0m5lBemANu3NLWx9WuNUHHhM1Ciw7iEK6Woja/3kj9sppdLUpbAn211FPz9Ih26s32CQEKquBPSPo662n6xA==',
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive',
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'Origin': 'https://fanyi.baidu.com',
    'Pragma': 'no-cache',
    'Referer': 'https://fanyi.baidu.com/?fr=allproduct',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'same-origin',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0',
    'X-Requested-With': 'XMLHttpRequest',
    'sec-ch-ua': '"Not)A;Brand";v="99", "Microsoft Edge";v="127", "Chromium";v="127"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
}

params = {
    'from': 'zh',
    'to': 'en',
}


def v2transapi(query):
    sign = execjs.compile(open('baidu.js', 'r', encoding='utf-8').read()).call('getSign', query)
    data = {
        'from': params['from'],
        'to': params['to'],
        'query': query,
        'transtype': 'realtime',
        'simple_means_flag': '3',
        'sign': sign,
        'token': 'f62087190cb6da67c00c77f8558d5565',
        'domain': 'common',
        'ts': timestamp,
    }
    response = requests.post('https://fanyi.baidu.com/v2transapi', params=params, cookies=cookies, headers=headers,
                             data=data)
    return response.json()['trans_result']['data'][0]['dst']


if __name__ == '__main__':
    print(v2transapi("大家好，我是张春林，我们来自北京"))
